 (function($) {

$.welcome = {};


	
$.welcome.window = null;	

$.welcome.prepare = function(callback)
{
	$.post("welcome.php", null, function(data)
	{
		$.welcome.window = $(data);
		
		var $f =  $.welcome.window;
		
		$f.dialog(
			 {
				draggable: false,
//				height: '',
				width: '450',
				modal: true,
				resizable: false,
				title: 'Welcome',
		//		show: 'fade',
				autoOpen: false,
				close : $.welcome.verifyAndClose,
				resize: 'auto'
			 });
			 
		$("#btnSignup").click($.welcome.signUp);
		
		$("#btnLogin").click(function() { $.welcome.post(); });
		
		$("#btnLetMePLay").click($.welcome.guest);
		
		$("#btnLogin").button('option', 'label', 'Login').addClass("ui-state-hover"); 
	
		$("#btnLogin").hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).addClass("ui-state-hover"); 
		});

		$("input:text, input:password, input:checkbox").keypress(function(e)
		{
			if(e.which == 13)
			{
				$('#btnLogin').click();
				e.preventDefault();
			}
		});	
		
		$(".button").button().disableSelection();
		
		if(typeof(callback) == 'function')
			callback();
			
	}, "html");
}

$.welcome.guest = function()
{
	$.welcome.post("guest=1");
}

$.welcome.verifyAndClose = function()
{
	if($.cookie('Email') == null)
		$.welcome.guest();
	else
	{
		$.elements.loadElements();

		$.news.loadNewElements ('clear');
		
		$("#welcomeMessage").attr('closed', true);
	}
}

$.welcome.close = function()
{
	$("#welcomeMessage").dialog('close');
}

$.welcome.signUp = function()
{
	if($("#btnSignup").text() == "Sign up")
	{
		$.welcome.showSignup();
	}
	else 
	{
		$.welcome.showLogin();
	}
}

$.welcome.post = function(data, callback)
{
	if(data == null)
		data = $("form").serialize();

	if(typeof(callback) != 'function')
	{
		callback = function (data) { 
			if(data.sucess)
			{
				$.welcome.close();
			}
			else
			{
				$("#loginMessage").html(data.msg);
				
				$("#loginMessage").fadeIn('fast');
			}
		
		}
	}
		
	$.post("./bus/login.php", data,  callback);
}

$.welcome.showLogin = function()
{
	$("input").val("");
	if($.welcome.window == null)
		$.welcome.prepare($.welcome.setFormForLogin);
	else
		$.welcome.setFormForLogin();	
}

$.welcome.setFormForLogin = function()
{
	$("#hdnSignup").val(0);		
	
	$("#btnSignup").button('option', 'label', 'Sign up');
	
	if($("#welcomeMessage").dialog('isOpen'))
	{
		dialog = $("#welcomeMessage").closest('.ui-dialog');
		dialog.animate({ top: (document.height / 2) - (dialog.height() / 2) + ($("#retypePassword").height()  / 2)}, 'fast'); 
		
		$("#retypePassword").animate({ height: 'hide' }, 'fast');
		
		$("#btnLogin").button('option', 'label', 'Login');
		$("#btnRecoverPassword").animate({ height: 'show' }, 'fast');
	}
	else
	{
		$("#welcomeMessage").attr('closed', false);
		$("#welcomeMessage").dialog('open');
	}	
}


$.welcome.showSignup = function()
{
	
	$("#hdnSignup").val(1);	
	
	$("#btnLogin").button('option', 'label', 'Sign up');
	
	$("#btnSignup").button('option', 'label', 'Back to Login');

	dialog = $("#welcomeMessage").closest('.ui-dialog');
	dialog.animate({ top: (document.height / 2) - (dialog.height() / 2) - 40   },'fast'); 
	
	$("#retypePassword").animate({ height: 'show'}, 'fast');
	$("#btnRecoverPassword").animate({ height: 'hide' }, 'fast');
	
	
	
	//$("#welcomeMessage").animate({ height: 'show'}, 'fast').css('top', 100);	
}


})(jQuery);
	
